/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.DOMSettingItem={
	Init: function(){
		this.C.bClear.unbind('click').bind('click',this,this.eventClickClear );
		this.C.bApply.unbind('click').bind('click',this,this.eventClickbApply );
		return this;
	},
	eventClickbApply : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Apply.call(o);
	},
	eventClickClear : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Clear.call(o);
	},
	SetName: function(value)
	{
		this.P.SettingName = value;
		this.C.settingName[0].innerHTML = value;
		return this;
	},
	SetValue:function(value)
	{
		if (value != undefined || value != null) {
			this.C.value.val(value);
		}else
		{
			this.C.value.val('');
		}
		return this;
	},
	SetNotEmpty:function(value)
	{
		this.P.notEmpty = value == true;
		if (this.P.notEmpty == true) {
			this.C.bClear.hide();
		}else
		{
			this.C.bClear.show();
		}
		
		return this;
	},
	GetValue:function()
	{
		return this.C.value.val();
	},
	SetNode: function(node)
	{
		this.P.Node = node;
		return this;
	},	
	SetDOMSettingObject: function(o)
	{
		if (o)
		{
			this.SetValue(o.value).SetNotEmpty(o.notEmpty);
		}
		return this;
	},
	Clear:function()
	{
		this.C.value.val('');
		jsForms.DesignMode.RemoveDOMSetting(this.P.Node,this.P.SettingName);
		return this;
	},
	Apply:function()
	{
		if (this.P.Node)
		{
			var value = this.GetValue();
			if (this.P.notEmpty == true && (value == undefined || value.toString().length ==0))
			{
				return this;
			}
			jsForms.DesignMode.SetDOMSetting(this.P.Node, this.P.SettingName, value);
			this._ExecEvent('onApply',this);
		}
		return this;
	}
}
jsForms.Components.EventItem={
	Init:function()
	{
		this.C.bClear.unbind('click').bind('click',this,this.eventClickClear );
		this.C.bEnable.unbind('click').bind('click',this,this.eventClickEnable );
		this.C.bApply.unbind('click').bind('click',this,this.eventClickbApply );
		return this;
	},
	eventClickbApply : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Apply.call(o);
	},
	eventClickEnable : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.SetDisable.call(o,!o.P.IsDisabled);
		o.Apply.call(o);
	},
	eventClickClear : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Clear.call(o);
	},
	SetDisabled:function(value)
	{
		if (!this.P.EventObject)
		{
			jsForms.error('Event object not defined');
		}
		value = (value == true);
		if (value== true)
		{
			this.C.value.attr('disabled','1');
		}
		else
		{
			this.C.value.removeAttr('disabled');
		}
		this.P.EventObject.Disabled = value;
		return this;
	},
	UpdateName: function(value)
	{
		this.C.name[0].innerHTML = value;
		return this;
	},
	UpdateValue:function(value)
	{
		if (value != undefined || value != null) {
			this.C.value.val(value);
		}else
		{
			this.C.value.val('');
		}
		return this;
	},
	GetValue:function()
	{
		return this.C.value.val();
	},
	SetValueList: function (list)
	{
		this.C.value.empty();
		$('<option>').attr('value','').text('NONE').appendTo(this.C.value);
		for (var i in list)
		{
			if (typeof(list[i]) == 'function') {
				$('<option>').attr('value', i).text(i).appendTo(this.C.value);
			}
		}
		return this;
	},
	SetNotDtoC:function(value)
	{
		value = value == true;
		if (value == true)
		{
			this.C.bApply.hide();
			this.C.bEnable.hide();
			this.SetDisabled(true);
		}else
		{
			this.C.bApply.show();
			this.C.bEnable.show();
		}
		return this;
	},
	SetEventObject:function(o)
	{
		this.P.EventObject = o;
		if (o)
		{
			this.UpdateName(o.Info.Name).UpdateValue(o.Value).SetDisabled(o.Disabled).SetNotDtoC(o.Info.isNotDefinedToComponent);
		}
		return this;
	},
	Clear:function()
	{
		if (!this.P.EventObject)
		{
			jsForms.error('Event object not defined');
		}
		this.UpdateValue('');
		jsForms.DesignMode.RemoveEvent(this.P.EventObject);
		return this;
	},
	Apply:function()
	{
		if (!this.P.EventObject)
		{
			jsForms.error('Event object not defined');
		}
		var value = this.GetValue();
		if (value == "") {
			jsForms.DesignMode.RemoveEvent(this.P.EventObject);
		}
		else
		{
			this.P.EventObject.Value = value;
			jsForms.DesignMode.SetEvent(this.P.EventObject);
		}
		this._ExecEvent('onApply',this);
		return this;
	}
}
jsForms.Components.PropertyItem={
	Init:function()
	{
		this.C.bClear.unbind('click').bind('click',this,this.eventClickClear );
		this.C.bEnable.unbind('click').bind('click',this,this.eventClickEnable );
		this.C.bApply.unbind('click').bind('click',this,this.eventClickbApply );
		return this;
	},
	eventClickbApply : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Apply.call(o);
	},
	eventClickEnable : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.SetDisable.call(o,!o.Properties.IsDisable);
		o.Apply.call(o);
	},
	eventClickClear : function(e)
	{
		if (!e.data) {return}
		var o = e.data;
		o.Clear.call(o);
	},
	SetNotDtoC:function(value)
	{
		value = value == true;
		if (value == true)
		{
			this.C.bApply.hide();
			this.C.bEnable.hide();
			this.SetDisabled(true);
		}else
		{
			this.C.bApply.show();
			this.C.bEnable.show();
		}
		return this;
	},
	SetDisabled:function(value)
	{
		value = (value == true);
		if (value== true)
		{
			this.C.value.attr('disabled','1');
		}
		else
		{
			this.C.value.removeAttr('disabled');
		}
		if (!this.P.PropertyObject)
		{
			jsForms.error('Property object not defined');
		}
		this.P.PropertyObject.Disabled = value;
		return this;
	},
	UpdateName: jsForms.Components.EventItem.UpdateName,
	UpdateValue:jsForms.Components.EventItem.UpdateValue,
	GetValue:jsForms.Components.EventItem.GetValue,
	SetPropertyObject:function(o)
	{
		this.P.PropertyObject = o;
		if (o)
		{
			this.UpdateName(o.Info.Name).UpdateValue(o.Value).SetDisabled(o.Disabled).SetNotDtoC(o.Info.isNotDefinedToComponent);
		}
		return this;
	},
	Clear:function()
	{
		if (!this.P.PropertyObject)
		{
			jsForms.error('Property object not defined');
		}
		this.UpdateValue('');
		jsForms.DesignMode.RemoveProperty(this.P.PropertyObject);
		return this;
	},
	Apply:function()
	{
		if (!this.P.PropertyObject)
		{
			jsForms.error('Property object not defined');
		}
		this.P.PropertyObject.Value = this.GetValue();
		jsForms.DesignMode.SetProperty(this.P.PropertyObject);
		this._ExecEvent('onApply',this);
		return this;
	}
};
